#include "cust_acc_lost_apply.h"
#include "ui_cust_acc_lost_apply.h"
#include "QSqlDatabase"
#include "QDebug"
#include "QSqlQuery"
#include "QVariant"
#include "QComboBox"
#include "QDateTime"
#include "QMessageBox"

cust_acc_lost_apply::cust_acc_lost_apply(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::cust_acc_lost_apply)
{
    ui->setupUi(this);
    this->setWindowTitle("申请挂失");
}

cust_acc_lost_apply::~cust_acc_lost_apply()
{
    delete ui;
}

void cust_acc_lost_apply::on_admit_clicked()
{
    QSqlDatabase sqlite_dep = QSqlDatabase::database("deposit");
    QSqlQuery query_dep(sqlite_dep);
    query_dep.prepare("SELECT savingsamount, savingstype FROM DEPOSIT WHERE custtelNumber = ?");
    query_dep.addBindValue(ui->user_card_id->text());
    query_dep.exec();
    query_dep.next();
    if(query_dep.value(0).toString() == ui->user_savings_amount->text() && query_dep.value(1).toString() == ui->user_savings_type->text())
    {
        //挂失成功
        query_dep.prepare("UPDATE DEPOSIT SET islost = ?, losttime = ? WHERE custtelnumber = ? ");
        query_dep.addBindValue("挂失中");
        query_dep.addBindValue("挂失中"+QDateTime::currentDateTime().toString());
        query_dep.addBindValue(ui->user_card_id->text());
        query_dep.exec();

        QMessageBox box;
        box.setWindowTitle("completed");
        box.setText("挂失成功");
        box.setIcon(QMessageBox::Information);
        box.exec();
        return;
    }
    else
    {
        //处理不存在此存款情况
        QSqlDatabase sqlite_ts = QSqlDatabase::database("transaction");
        QSqlQuery query_ts(sqlite_ts);
        query_ts.prepare("SELECT custtelnumber,transactionamount FROM TRANS WHERE custtelNumber = ?");
        query_ts.addBindValue(ui->user_card_id->text());
        query_ts.exec();
        while(query_ts.next())
        {

            if(query_ts.value(0).toString() == ui->user_card_id->text()
                    && query_ts.value(1).toString() == ui->user_savings_amount->text())
            {
                QMessageBox box;
                box.setWindowTitle("warning");
                box.setText("该笔存款已被取走");
                box.setIcon(QMessageBox::Warning);
                box.exec();
                return;
            }

        }
        QMessageBox box;
        box.setWindowTitle("warning");
        box.setText("您未进行过该笔存款");
        box.setIcon(QMessageBox::Warning);
        box.exec();
        return;
    }
    //query_dep.prepare("UPDATE DEPOSIT SET savingstype = ?, savingsamount = ?, savingstime = ? WHERE custtelnumber = ? ");
    //query_dep.addBindValue(ui->comboBox->currentText());
    //query_dep.addBindValue(ui->savings_amount->text());
    //query_dep.addBindValue(QDateTime::currentDateTime().toString());
    //query_dep.addBindValue(ui->user_card_id->text());
    //if(query_dep.exec()) qDebug() << "存款成功";
}
